var interval;
var pcinterval;
$(function() {
     $("#corpType li").on('click', function(event) {
        $("#corpType li").removeClass("active");
        $(this).addClass("active");
        lastPrice();
    });

    $("#serviceType li").on('click', function(event) {
        $("#serviceType li").removeClass("active");
        $(this).addClass("active");
        lastPrice();
    });

    $("#officialDetial li").click(function(){
        data = $(this).attr("data");
        $("#officialDetial li").removeClass("active");
        $(this).addClass("active");
        $("#card-item-1").hide();
        $("#card-item-2").hide();
        if (data == 2) {
            var appid = $("#appid").text();
            window.location.href = "/admin/miniapps/miniapp_lists?appid="+appid;
        }
        $("#card-item-"+data).show();
    });
});

function lastPrice() {
    var corpType = $("#corpType .active").attr("data");
    var serviceType = $("#serviceType .active").attr("data");
    if (corpType == 1) {
        var authPrice = 0;
    } else if (corpType == 2) {
        var authPrice = 0;
    } else {
        var authPrice = 0;
    }
    // if (serviceType == 1) {
    //     var servicePrice = 100;
    //     $("#serviceTypeRow").show().html('客服全程跟进指导费用<font id="servicePrice">¥'+servicePrice+'</font>元。');
    // } else if (serviceType == 2) {
    //     var servicePrice = 100;
    //     $("#serviceTypeRow").show().html('经理全程代备案费用<font id="servicePrice">¥'+servicePrice+'</font>元。');
    // } else {
    //     var servicePrice = 100;
    //     $("#serviceTypeRow").show().html('内部快速备案通道<font id="servicePrice">¥'+servicePrice+'</font>元。');
    // }
    var servicePrice = 100;
    $("#serviceTypeRow").show().html('内部快速备案通道<font id="servicePrice">¥'+servicePrice+'</font>元。');
    $("#servicePrice").text("¥"+servicePrice);
    $("#authPrice").text("¥"+authPrice);
    $("#lastPrice").text(authPrice+servicePrice);
}

function orderPay() {
    var corp_type = $("#corpType .active").attr("data");
    var service_type = $("#serviceType .active").attr("data");
    $.ajax({
        "url": "/admin/order/pay_submit",
        "type": "GET",
        "data": {"corp_type":corp_type,"service_type":service_type},
        success: function(result){
            if (result.status == "success") {
                $("#preview_qrcode").hide().html("");
                var qrcode = new QRCode(document.getElementById("preview_qrcode"), {
                    text: result.data.code_url,
                    width: 150,
                    height: 150,
                    colorDark : "#000000",
                    colorLight : "#ffffff",
                    correctLevel : QRCode.CorrectLevel.H
                });
                $("#preview_qrcode").show();
                $("#order_id").val(result.data.out_trade_no);
                $("#orderPayModal").modal("toggle");
                timeLeft();
            } else {
                mviewToastr("error",result.msg,5);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            colsemviewToastr();
            mviewToastr("error","网页错误,请稍后尝试!");
        }
    });
}

function pcOrderPay() {
    var corp_type = $("#corpType .active").attr("data");
    var service_type = $("#serviceType .active").attr("data");
    $.ajax({
        "url": "/admin/order/pay_submit",
        "type": "GET",
        "data": {"corp_type":corp_type,"service_type":service_type},
        success: function(result){
            if (result.status == "success") {
                $("#preview_qrcode").hide().html("");
                var qrcode = new QRCode(document.getElementById("preview_qrcode"), {
                    text: result.data.code_url,
                    width: 150,
                    height: 150,
                    colorDark : "#000000",
                    colorLight : "#ffffff",
                    correctLevel : QRCode.CorrectLevel.H
                });
                $("#preview_qrcode").show();
                $("#order_id").val(result.data.out_trade_no);
                $("#orderPayModal").modal("toggle");
                pcTimeLeft();
            } else {
                mviewToastr("error",result.msg,5);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            colsemviewToastr();
            mviewToastr("error","网页错误,请稍后尝试!");
        }
    });
}

function pcTimeLeft() {
    var countdown = 300;
    var reqtime = 1;
    pcinterval = setInterval(function() {
        if (countdown >= 60) {
            var minutes = parseInt(countdown/60);
            var time = minutes+"分"+(countdown-minutes*60)+"秒";
        } else {
            var time = countdown+"秒";
        }
        $('#admin_service_time font').text(time);
        reqtime += 1;
        if (reqtime == 4) {
            reqtime = 1;
            pcOrderPayStatus();
        }
        if (--countdown < 0) {
            clearInterval(pcinterval);
            closeOrderPay();
        }
    }, 1000);
}

function pcOrderPayStatus() {
    var order_id = $("#order_id").val();
    var service_type = $("#serviceType .active").attr("data");
    $.ajax({
        "url": "/admin/order/pay_status",
        "type": "GET",
        "data": {"order_id":order_id},
        success: function(result){
            if (result.status == "success") {
                mviewToastr("success","支付成功,添加微信客服!");
                clearInterval(pcinterval);
                if (service_type == 1) {
                    $("#kefuFrom img").attr("src","https://wework.qpic.cn/wwpic3az/383731_yuObxVW9QzCConi_1724988237/0");
                } else if (service_type == 2) {
                    $("#kefuFrom img").attr("src","https://wework.qpic.cn/wwpic3az/878227_p_z5ZO-FQ8W9QnV_1724988281/0");
                } else {
                    $("#kefuFrom img").attr("src","https://wework.qpic.cn/wwpic3az/357324_SBswFvmWQ-ernCR_1724988316/0");
                }
                $("#orderPayModal").modal("toggle");
                $("#payFrom").hide();
                $("#kefuFrom").show();
            }
        }
    });
}

function closeOrderPay() {
    $("#orderPayModal").modal("toggle");
}

function timeLeft() {
    var countdown = 300;
    var reqtime = 1;
    interval = setInterval(function() {
        if (countdown >= 60) {
            var minutes = parseInt(countdown/60);
            var time = minutes+"分"+(countdown-minutes*60)+"秒";
        } else {
            var time = countdown+"秒";
        }
        $('#admin_service_time font').text(time);
        reqtime += 1;
        if (reqtime == 4) {
            reqtime = 1;
            orderPayStatus();
        }
        if (--countdown < 0) {
            clearInterval(interval);
            closeOrderPay();
        }
    }, 1000);
}

function orderPayStatus() {
    var order_id = $("#order_id").val();
    $.ajax({
        "url": "/admin/order/pay_status",
        "type": "GET",
        "data": {"order_id":order_id},
        success: function(result){
            if (result.status == "success") {
                mviewToastr("success","支付成功,正在跳转");
                clearInterval(interval);
                window.location.href = "/admin/miniapps/miniapp_register";
            }
        }
    });
}

function confirmMviewNotification() {
    var mviewNotificationData = $("#mviewNotificationData").val();
    var notificationData = JSON.parse(mviewNotificationData);
    if (notificationData.type == "official_delete") {
        window.location.href = "/admin/miniapps/official_delete/"+notificationData.appid;
    } else if (notificationData.type == "mini_replace") {
        registerMiniappBind();
    }
    colseMviewNotification();
}

function miniListAdd() {
    var enterprise_name = $("#enterprise_name").val();
    if (enterprise_name == "") {
        mviewToastr("error","请输入企业名称!");
        $("#enterprise_name").focus();
        return;
    }
    var code = $("#code").val();
    if (code == "") {
        mviewToastr("error","请输入企业代码!");
        $("#code").focus();
        return;
    }
    var legal_persona_name = $("#legal_persona_name").val();
    if (legal_persona_name == "") {
        mviewToastr("error","请输入企业法人姓名!");
        $("#legal_persona_name").focus();
        return;
    }
    var legal_persona_wechat = $("#legal_persona_wechat").val();
    if (legal_persona_wechat == "") {
        mviewToastr("error","请输入企业法人微信!");
        $("#legal_persona_wechat").focus();
        return;
    }
    var code_type = $("#code_type").val();
    mviewToastr("load","正在添加中,请稍等...",10);
    $.ajax({
        "url": "/admin/template/mini_add_submit",
        "type": "GET",
        "data": {"enterprise_name":enterprise_name,"code":code,"legal_persona_name":legal_persona_name,"legal_persona_wechat":legal_persona_wechat,"code_type":code_type},
        success: function(res){
            colsemviewToastr();
            if (res.status == "success") {
                mviewToastr("success",res.msg);
                setTimeout(function(){
                    window.location.href = "/admin/template/mini";
                },1000);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            colsemviewToastr();
            mviewToastr("error","网页错误,请稍后尝试!");
        }
    });
}

function miniListUpdate() {
    var enterprise_name = $("#enterprise_name").val();
    if (enterprise_name == "") {
        mviewToastr("error","请输入企业名称!");
        $("#enterprise_name").focus();
        return;
    }
    var code = $("#code").val();
    if (code == "") {
        mviewToastr("error","请输入企业代码!");
        $("#code").focus();
        return;
    }
    var legal_persona_name = $("#legal_persona_name").val();
    if (legal_persona_name == "") {
        mviewToastr("error","请输入企业法人姓名!");
        $("#legal_persona_name").focus();
        return;
    }
    var legal_persona_wechat = $("#legal_persona_wechat").val();
    if (legal_persona_wechat == "") {
        mviewToastr("error","请输入企业法人微信!");
        $("#legal_persona_wechat").focus();
        return;
    }
    var code_type = $("#code_type").val();
    var id = $("#id").val();
    mviewToastr("load","正在更新中,请稍等...",10);
    $.ajax({
        "url": "/admin/template/mini_update_submit",
        "type": "GET",
        "data": {"id":id,"enterprise_name":enterprise_name,"code":code,"legal_persona_name":legal_persona_name,"legal_persona_wechat":legal_persona_wechat,"code_type":code_type},
        success: function(res){
            colsemviewToastr();
            if (res.status == "success") {
                mviewToastr("success",res.msg);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            colsemviewToastr();
            mviewToastr("error","网页错误,请稍后尝试!");
        }
    });
}

function miniListDelete() {
    var id = $("#id").val();
    $.ajax({
        "url": "/admin/template/mini_delete",
        "type": "GET",
        "data": {"id":id},
        success: function(res){
            if (res.status == "success") {
                mviewToastr("success",res.msg);
                setTimeout(function() {
                    window.location.href = "/admin/template/mini";
                },1000);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            mviewToastr("error","网页错误,请稍后尝试!");
        }
    });
}

function keyListActive() {
    var key = $("#key").val();
    if (key == "") {
        mviewToastr("error","请输入激活码!");
        $("#key").focus();
        return;
    }
    if (key.length != 32) {
        mviewToastr("error","激活码错误,请输入正确的激活码!");
        $("#key").focus();
        return;
    }
    mviewToastr("load","激活中,请稍等...",10);
    $.ajax({
        "url": "/admin/order/key_active",
        "type": "GET",
        "data": {"key":key},
        success: function(res){
            colsemviewToastr();
            if (res.status == "success") {
                mviewToastr("success",res.msg);
                setTimeout(function(){
                    window.location.href = "/admin/users/users_center";
                },1000);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            colsemviewToastr();
            mviewToastr("error","网页错误,请稍后尝试!");
        }
    });
}

function adminListAdd() {
    var manage_email = $("#manage_email").val();
    if (manage_email == "") {
        mviewToastr("error","请输入管理员邮箱!");
        $("#manage_email").focus();
        return;
    }
    var manage_mobile = $("#manage_mobile").val();
    if (manage_mobile == "") {
        mviewToastr("error","请输入管理员手机号!");
        $("#manage_mobile").focus();
        return;
    }
    var manage_emergency_contact = $("#manage_emergency_contact").val();
    if (manage_emergency_contact == "") {
        mviewToastr("error","请输入管理员应急手机号!");
        $("#manage_emergency_contact").focus();
        return;
    }
    if (manage_mobile == manage_emergency_contact) {
        mviewToastr("error","管理员电话与应急联系电话不能相同!");
        return;
    }
    var manage_idcard = $("#manage_idcard").val();
    var manage_name = $("#manage_name").val();
    var manage_date_start = $("#manage_date_start").val();
    var manage_date_end = $("#manage_date_end").val();
    var manage_photo_front = $("#manage_photo_front_show").attr("src");
    var manage_photo_back = $("#manage_photo_back_show").attr("src");

    var currentDate = new Date();
    var year = currentDate.getFullYear();
    var month = currentDate.getMonth() + 1;
    var day = currentDate.getDate();
    var today = parseInt(year+month+day);
    if (manage_idcard == "") {
        mviewToastr("error","请上传清晰的身份证正面照!");
        return;
    }
    if (manage_date_start == "") {
        mviewToastr("error","请上传清晰的身份证反面照!");
        return;
    }
    if (today >= parseInt(manage_date_end)) {
        mviewToastr("error","身份证已过期,请上传未过期的身份证!");
        return;
    }
    mviewToastr("load","正在添加中,请稍等...",10);
    $.ajax({
        "url": "/admin/template/admin_add_submit",
        "type": "GET",
        "data": {"manage_name":manage_name,"manage_idcard":manage_idcard,"manage_date_start":manage_date_start,"manage_date_end":manage_date_end,"manage_email":manage_email,"manage_mobile":manage_mobile,"manage_emergency_contact":manage_emergency_contact,"manage_photo_front":manage_photo_front,"manage_photo_back":manage_photo_back},
        success: function(res){
            colsemviewToastr();
            if (res.status == "success") {
                mviewToastr("success",res.msg);
                setTimeout(function(){
                    window.location.href = "/admin/template/admin";
                },1000);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            colsemviewToastr();
            mviewToastr("error","网页错误,请稍后尝试!");
        }
    });
}

function jsUploadCallBack(id,imageurl,imageName,updata,other) {
    $.ajax({
        "url": "/admin/miniapp/miniapp_beian_image",
        "type": "GET",
        "data": {"status":id,"imgurl":imageurl},
        success: function(res){
            console.log(res);
            if (res.status == "error") {
                mviewToastr("error",res.msg);
                $("#"+id+"_show").attr("src","");
            } else {
                if (id == "manage_photo_front") {
                    $("#manage_name").val(res.data.name);
                    $("#manage_idcard").val(res.data.idcard);
                } else if (id == "manage_photo_back") {
                    $("#manage_date_start").val(res.data.date_start);
                    $("#manage_date_end").val(res.data.date_end);
                }
            }
        },
        error: function(err){
            $("#addbutton").html(btnText).attr("onclick","submitAdd()");
            mviewToastr("error","请求失败,请稍后重试！");
            $("#alert_success").hide();
        }
    });
}

function adminListUpdate() {
    var manage_email = $("#manage_email").val();
    if (manage_email == "") {
        mviewToastr("error","请输入管理员邮箱!");
        $("#manage_email").focus();
        return;
    }
    var manage_mobile = $("#manage_mobile").val();
    if (manage_mobile == "") {
        mviewToastr("error","请输入管理员手机号!");
        $("#manage_mobile").focus();
        return;
    }
    var manage_emergency_contact = $("#manage_emergency_contact").val();
    if (manage_emergency_contact == "") {
        mviewToastr("error","请输入管理员应急手机号!");
        $("#manage_emergency_contact").focus();
        return;
    }
    if (manage_mobile == manage_emergency_contact) {
        mviewToastr("error","管理员电话与应急联系电话不能相同!");
        return;
    }
    var id = $("#id").val();
    var manage_idcard = $("#manage_idcard").val();
    var manage_name = $("#manage_name").val();
    var manage_date_start = $("#manage_date_start").val();
    var manage_date_end = $("#manage_date_end").val();
    var manage_photo_front = $("#manage_photo_front_show").attr("src");
    var manage_photo_back = $("#manage_photo_back_show").attr("src");

    var currentDate = new Date();
    var year = currentDate.getFullYear();
    var month = currentDate.getMonth() + 1;
    var day = currentDate.getDate();
    var today = parseInt(year+month+day);
    if (manage_idcard == "") {
        mviewToastr("error","请上传清晰的身份证正面照!");
        return;
    }
    if (manage_date_start == "") {
        mviewToastr("error","请上传清晰的身份证反面照!");
        return;
    }
    if (today >= parseInt(manage_date_end)) {
        mviewToastr("error","身份证已过期,请上传未过期的身份证!");
        return;
    }
    mviewToastr("load","正在添加中,请稍等...",10);
    $.ajax({
        "url": "/admin/template/admin_update_submit",
        "type": "GET",
        "data": {"id":id,"manage_name":manage_name,"manage_idcard":manage_idcard,"manage_date_start":manage_date_start,"manage_date_end":manage_date_end,"manage_email":manage_email,"manage_mobile":manage_mobile,"manage_emergency_contact":manage_emergency_contact,"manage_photo_front":manage_photo_front,"manage_photo_back":manage_photo_back},
        success: function(res){
            colsemviewToastr();
            if (res.status == "success") {
                mviewToastr("success",res.msg);
                setTimeout(function(){
                    window.location.href = "/admin/template/admin";
                },1000);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            colsemviewToastr();
            mviewToastr("error","网页错误,请稍后尝试!");
        }
    });
}

function adminListDelete() {
    var id = $("#id").val();
    $.ajax({
        "url": "/admin/template/admin_delete",
        "type": "GET",
        "data": {"id":id},
        success: function(res){
            if (res.status == "success") {
                mviewToastr("success",res.msg);
                setTimeout(function() {
                    window.location.href = "/admin/template/admin";
                },1000);
            } else {
                mviewToastr("error",res.msg,5);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            mviewToastr("error","网页错误,请稍后尝试!");
        }
    });
}